#include "StdAfx.h"
#include "CanvasUI.h"
#include "BlueRenderEngineUI.h"

CCanvasUI::CCanvasUI(void) : m_hBitmap(NULL), m_iOrientation(HTTOPRIGHT)
{
}

CCanvasUI::~CCanvasUI(void)
{
	 if( m_hBitmap != NULL ) ::DeleteObject(m_hBitmap);
}

LPCTSTR CCanvasUI::GetClass() const
{
	return _T("CanvasUI");
}

bool CCanvasUI::SetWatermark(UINT iBitmapRes, int iOrientation)
{
	return SetWatermark(MAKEINTRESOURCE(iBitmapRes), iOrientation);
}

bool CCanvasUI::SetWatermark(LPCTSTR pstrBitmap, int iOrientation)
{
	if( m_hBitmap != NULL ) ::DeleteObject(m_hBitmap);
	m_hBitmap = (HBITMAP) ::LoadImage(m_pManager->GetResourceInstance(), pstrBitmap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
	ASSERT(m_hBitmap!=NULL);
	if( m_hBitmap == NULL ) return false;
	::GetObject(m_hBitmap, sizeof(BITMAP), &m_BitmapInfo);
	m_iOrientation = iOrientation;
	//m_iOrientation = HTTOPRIGHT;
	Invalidate();
	return true;
}

void CCanvasUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	// Fill background
	RECT rcFill = { 0 };
	if( ::IntersectRect(&rcFill, &rcPaint, &m_rcItem) ) {
		CBlueRenderEngineUI::DoFillRect(hDC, m_pManager, rcFill, m_clrBack);
	}
	// Paint watermark bitmap
	if( m_hBitmap != NULL ) {
		RECT rcBitmap = { 0 };
		switch( m_iOrientation ) {
	  case HTTOPRIGHT:
		  ::SetRect(&rcBitmap, m_rcItem.right - m_BitmapInfo.bmWidth, m_rcItem.top, m_rcItem.right, m_rcItem.top + m_BitmapInfo.bmHeight);
		  break;
	  case HTBOTTOMRIGHT:
		  ::SetRect(&rcBitmap, m_rcItem.right - m_BitmapInfo.bmWidth, m_rcItem.bottom - m_BitmapInfo.bmHeight, m_rcItem.right, m_rcItem.bottom);
		  break;
	  default:
		  ::SetRect(&rcBitmap, m_rcItem.right - m_BitmapInfo.bmWidth, m_rcItem.bottom - m_BitmapInfo.bmHeight, m_rcItem.right, m_rcItem.bottom);
		  break;
		}
		RECT rcTemp = { 0 };
		if( ::IntersectRect(&rcTemp, &rcPaint, &rcBitmap) ) {
			CRenderClip clip;
			CBlueRenderEngineUI::GenerateClip(hDC, m_rcItem, clip);
			CBlueRenderEngineUI::DoPaintBitmap(hDC, m_pManager, m_hBitmap, rcBitmap);
		}
	}
	CContainerUI::DoPaint(hDC, rcPaint);
}

void CCanvasUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if( _tcscmp(pstrName, _T("watermark")) == 0 ) SetWatermark(pstrValue);
	else CContainerUI::SetAttribute(pstrName, pstrValue);
}

